package com.hiwhitley.himei.contract.presenter;

import android.util.Log;

import com.hiwhitley.himei.contract.GameDetailContract;
import com.hiwhitley.himei.contract.model.GameDetailModel;
import com.hiwhitley.himei.entity.Game;
import com.hiwhitley.himei.entity.ScreenItem;
import com.hiwhitley.himei.entity.GameVideoHorizontal;
import com.hiwhitley.himei.entity.GameVideoVertical;
import com.hiwhitley.himei.mvpframe.rx.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;

/**
 * Created by hiwhitley on 2016/10/20.
 */

public class GameDetailPresenter extends GameDetailContract.Presenter {
    @Override
    public void loadGameDetail(int gid) {
        mRxManager.add(mModel.getGameDetail(gid)
                .compose(RxSchedulers.io_main())
                .subscribe(new Observer<Game>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Game game) {
                        mView.showGameDetail(game);
                        List<ScreenItem> itemList = new ArrayList<>();
                        if (game.isIs_vertical()) {
                            itemList.add(new GameVideoVertical()
                                    .setVideo(game.getVideo().get(0))
                                    .setGameName(game.getName_cn())
                                    .setGid(game.getId()));
                        } else {
                            itemList.add(new GameVideoHorizontal()
                                    .setVideo(game.getVideo().get(0))
                                    .setGameName(game.getName_cn())
                                    .setGid(game.getId()));
                        }

                        for (Game.Screens screens : game.getScreens()) {
                            Log.i("gameName", game.getName_cn());
                            itemList.add(GameDetailModel.parseDataByType(screens, game.isIs_vertical()));
                        }
                        mView.showGameScreens(itemList);
                    }
                })

        );
    }
}
